package util
{
	import flash.system.System;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.formatters.DateFormatter;
	
	import spark.components.DataGrid;
	import spark.components.gridClasses.CellPosition;

	public class CommonOp
	{
		public function CommonOp()
		{
		}
		public static function GetBaseUrl(swfUrl:String):String
		{
			var pos1:int = swfUrl.lastIndexOf("/");
			var urlParent:String = swfUrl.substr(0,pos1);
			var pos2:int= urlParent.lastIndexOf("/");
			var urlRealParent:String = urlParent.substr(0,pos2);
			return urlRealParent;
		}
		public static function doCopy(dgResult:DataGrid):void
		{
			var selCells:Vector.<CellPosition> = dgResult.selectedCells;
			if(selCells!=null)
			{
				var rowX:int = -1;
				var cpyString:String = "";
				for each( var cell:CellPosition in selCells)
				{
					if(rowX!=-1 && rowX!=cell.rowIndex)
					{
						cpyString+="\r\n";
					}
					else if(rowX!=-1)
					{
						cpyString+="\t";
					}
					rowX = cell.rowIndex;
					var objX:Object = dgResult.dataProvider.getItemAt(cell.rowIndex);
					var columns:ArrayList  = dgResult.columns as ArrayList;
					var vobj:Object = objX[columns.source[cell.columnIndex].dataField];
					if(vobj!=null)
					{
						cpyString+=vobj.toString();
					}
					
				}
				System.setClipboard(cpyString);
			}
		}
	
		public static function fillData(table:Object,targetArray:ArrayCollection):void
		{
			targetArray.removeAll();
			if(table!=null){
				var datas:Array = table.serverInfo.initialData;
				var cols:Array = table.serverInfo.columnNames;
				for (var i:int=0; i<datas.length; i++)
				{
					var obj:Object = new Object();
					for (var j:int =0; j<cols.length; j++)
					{
						obj[cols[j]] = datas[i][j];
					}
					targetArray.addItem(obj);
				}
			}
		}
		/*
		通过名称来获取相应的下拉列表索引
		*/
		public static function getIndexByName(list:ArrayCollection,name:String, key:String):int
		{ 
			var index:int = -1; 
			for(var i:int = 0; i<list.length; i++){ 
				var obj:Object = list[i];
				if(obj[name] == key){ 
					index = i;
					break; 
				} 
			}
			return index; 
		}
		
		public static function getChinaTimeDate(nowDate:Date):String
		{
			var rtnStr:String = "";
			// 格式化函数
			
			var myDateFormatXQ = new DateFormatter();
			var testY:String = nowDate.getFullYear().toString();
			var testM:String = (nowDate.getMonth()+1).toString();
			var testD:String = nowDate.getDate().toString();
			var weekStr = "";
			var ymd:String = "";
			// 定义日期格式
			
			myDateFormatXQ.formatString="EEEE";
			
			weekStr = myDateFormatXQ.format(nowDate);
			
			switch(weekStr)
			{                            
				case "Monday":
					weekStr = "星期一";
					break;
				case "Tuesday":
					weekStr = "星期二";
					break;
				case "Wednesday":
					weekStr = "星期三";
					break;        
				case "Thursday":
					weekStr = "星期四";
					break;    
				case "Friday":
					weekStr = "星期五";
					break;    
				case "Saturday":
					weekStr = "星期六";
					break;    
				case "Sunday":
					weekStr = "星期日";
					break;                
			}
			rtnStr = testY+"年"+testM+"月"+testD+"日";
			rtnStr = rtnStr + " " +weekStr;
		
			return rtnStr;
			
		}
	}
}